home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / program / jfklib.zip / WINDOWS.CPP < prev    next >
Text File  |  1991-05-11  |  5KB  |  262 lines

  1. /*
  2.     WINDOWS.CPP - (C) 1990 by Joachim Kainz 'On a mission from Bhudda'
  3. */
  4.     #include "windows.hpp"
  5.     #include <string.h>
  6.  
  7.     WORD     WINDOW::wCount            = 0;
  8.     FARPROC2 WINDOW::lpFnOOPWndProc = (FARPROC2) NULL;
  9.     MSGLOOP  WINDOW::msg;
  10.  
  11.     static BOOL RegisterClass (
  12.                     WORD     wStyle,
  13.                     HICON     hCursor,
  14.                     HCURSOR  hIcon,
  15.                     HBRUSH     hBackground,
  16.                     WORD     wClsExtra,
  17.                     WORD     wWndExtra,
  18.                     LPSTR     lpClass,
  19.                     FARPROC2 lpFnProc
  20.                 );
  21.  
  22.     EXPORT WINDOW::WINDOW (
  23.                 WORD        wStyle,
  24.                 HCURSOR        hCursor,
  25.                 HICON        hIcon,
  26.                 HBRUSH        hBackground,
  27.                 WORD        wClsExtra,
  28.                 WORD        wWndExtra,
  29.                 LPSTR        lpClass,
  30.                 FARPROC2    lpFnProc
  31.     ) {
  32.         hWnd = NULL;
  33.  
  34.         RegisterClass (
  35.             wStyle,
  36.             hCursor,
  37.             hIcon,
  38.             hBackground,
  39.             wClsExtra,
  40.             wWndExtra,
  41.             lpClass,
  42.             lpFnProc
  43.         );
  44.     }
  45.  
  46.     EXPORT WINDOW::WINDOW (
  47.                 int            nCmdShow,
  48.                 int            x,
  49.                 int            y,
  50.                 int            cx,
  51.                 int            cy,
  52.                 long        lStyle,
  53.                 LPSTR       lpName,
  54.                 HANDLE        hMenu,
  55.                 HANDLE        hParent,
  56.                 long        lExStyle,
  57.                 WORD        wStyle,
  58.                 HCURSOR        hCursor,
  59.                 HICON        hIcon,
  60.                 HBRUSH        hBackground,
  61.                 WORD        wClsExtra,
  62.                 WORD        wWndExtra,
  63.                 LPSTR        lpParam,
  64.                 LPSTR        lpClass,
  65.                 FARPROC2    lpFnProc
  66.     ) {
  67.         hWnd = NULL;
  68.  
  69.         if (
  70.             !RegisterClass (
  71.                 wStyle,
  72.                 hCursor,
  73.                 hIcon,
  74.                 hBackground,
  75.                 wClsExtra,
  76.                 wWndExtra,
  77.                 lpClass,
  78.                 lpFnProc
  79.              )
  80.         )
  81.             return;
  82.  
  83.         CREATESTRUCT cs;
  84.  
  85.         cs.lpCreateParams    = lpParam;
  86.         cs.hInstance        = GetInstance ();
  87.         cs.hMenu            = hMenu;
  88.         cs.hwndParent        = hParent;
  89.         cs.cy                = cy;
  90.         cs.cx                = cx;
  91.         cs.y                = y;
  92.         cs.x                = x;
  93.         cs.style            = lStyle;
  94.         cs.lpszName            = lpName;
  95.         cs.lpszClass        = lpClass;
  96.         cs.dwExStyle        = lExStyle;
  97.  
  98.         hWnd = CreateWindowEx (
  99.                     cs.dwExStyle,
  100.                     cs.lpszClass,
  101.                     cs.lpszName,
  102.                     cs.style,
  103.                     cs.x,
  104.                     cs.y,
  105.                     cs.cx,
  106.                     cs.cy,
  107.                     cs.hwndParent,
  108.                     cs.hMenu,
  109.                     cs.hInstance,
  110.                     cs.lpCreateParams
  111.                );
  112.  
  113.         SetWindowLong (GetWindowHandle (), 0, (long) this);
  114.  
  115.         lpDefWndProc =
  116.                 (FARPROC2) SetWindowLong (
  117.                                 GetWindowHandle (),
  118.                                 GWL_WNDPROC,
  119.                                 (long) GetOOPWndProc ()
  120.                            );
  121.         SendMessage    (self, WM_CREATE, NULL, (long) &cs);
  122.  
  123.         if (nCmdShow != SW_HIDE)
  124.             ShowWindow(GetWindowHandle (), nCmdShow);
  125.     }
  126.  
  127.     BOOL RegisterClass (
  128.             WORD     wStyle,
  129.             HICON     hCursor,
  130.             HCURSOR  hIcon,
  131.             HBRUSH     hBackground,
  132.             WORD     wClsExtra,
  133.             WORD     wWndExtra,
  134.             LPSTR     lpClass,
  135.             FARPROC2 lpFnProc
  136.          )
  137.     {
  138.         WNDCLASS wc;
  139.  
  140.         if (GetClassInfo (GetInstance (), lpClass, &wc))
  141.             return TRUE;
  142.  
  143.         wc.style        = wStyle;
  144.         wc.lpfnWndProc    = lpFnProc;
  145.         wc.cbClsExtra    = wClsExtra;
  146.         wc.cbWndExtra    = wWndExtra+sizeof (long); // Room for Pointer to
  147.         wc.hInstance    = GetInstance ();           // the instance
  148.         wc.hIcon        = hIcon;
  149.         wc.hCursor        = hCursor;
  150.         wc.hbrBackground= hBackground;
  151.         wc.lpszMenuName    = NULL;
  152.         wc.lpszClassName= lpClass;
  153.  
  154.         return RegisterClass (&wc);
  155.     }
  156.  
  157.     EXPORT WINDOW::~WINDOW()
  158.     {
  159.         if(!GetWindowHandle ())
  160.             if(IsWindow(GetWindowHandle ()))
  161.                 DestroyWindow(GetWindowHandle ());
  162.  
  163.         if (!GetCount ())
  164.             FreeProcInstance ((FARPROC) GetOOPWndProc ());
  165.     }
  166.  
  167.     int EXPORT MsgLoop (const WINDOW & wnd)
  168.     {
  169.         return wnd.msg.Loop ();
  170.     }
  171.  
  172.     FARPROC2 WINDOW::GetOOPWndProc () const
  173.     {
  174.         if (!lpFnOOPWndProc)
  175.              lpFnOOPWndProc = (FARPROC2) MakeProcInstance (
  176.                                             (FARPROC) DefOOPWndProc,
  177.                                             GetInstance ()
  178.                                          );
  179.  
  180.         return lpFnOOPWndProc;
  181.     }
  182.  
  183.     void EXPORT ShowWindow  (const WINDOW &wnd, int nShow)
  184.     {
  185.         ::ShowWindow (wnd.GetWindowHandle (), nShow);
  186.     }
  187.  
  188.     BOOL EXPORT IsWindow (const WINDOW &wnd)
  189.     {
  190.         if (!wnd.GetWindowHandle ())
  191.             return FALSE;
  192.  
  193.         return IsWindow (wnd.GetWindowHandle ());
  194.     }
  195.  
  196.     long EXPORT SendMessage (
  197.                  const WINDOW &wnd,
  198.                  WORD           wMsg,
  199.                  WORD           wParam,
  200.                  LONG           lParam
  201.                 )
  202.     {
  203.         return SendMessage (
  204.                 wnd.GetWindowHandle (),
  205.                 wMsg,
  206.                 wParam,
  207.                 lParam
  208.                );
  209.     }
  210.  
  211.     long EXPORT PostMessage (
  212.                     const WINDOW &wnd,
  213.                     WORD           wMsg,
  214.                     WORD           wParam,
  215.                     LONG           lParam
  216.                 )
  217.     {
  218.         return PostMessage (
  219.                 wnd.GetWindowHandle (),
  220.                 wMsg,
  221.                 wParam,
  222.                 lParam
  223.                );
  224.     }
  225.  
  226.     void EXPORT SetWindowText (const WINDOW &wnd, LPSTR lpText)
  227.     {
  228.         if (HIWORD (lpText)) {
  229.  
  230.             SetWindowText (
  231.                 wnd.GetWindowHandle (),
  232.                 lpText
  233.             );
  234.  
  235.             return;
  236.  
  237.         }
  238.  
  239.         PSTR pText;
  240.  
  241.         for (WORD wLen=64;; wLen*=2) {
  242.  
  243.             pText = new char [wLen];
  244.  
  245.             if (
  246.                 LoadString (
  247.                     GetInstance (),
  248.                     LOWORD (lpText),
  249.                     pText,
  250.                     wLen
  251.                 ) < wLen-1
  252.             )
  253.                 break;
  254.  
  255.             delete pText;
  256.  
  257.         }
  258.  
  259.         SetWindowText (wnd.GetWindowHandle (), pText);
  260.  
  261.         delete pText;
  262.     }